<?php
namespace Home\Controller;
use Home\Biz\DictBiz;
use Home\Biz\AdminBiz;
use Home\Biz\AuthBiz;
use Home\Biz\CommonBiz;
use Home\Biz\InvoiceBiz;
use Home\Biz\JobinfoBiz;
use Home\Biz\MeetingBiz;
use Home\Biz\ProvinceCityDistrictBiz;
use Home\Biz\VoteBiz;
use Home\Biz\WechatBiz;
use Think\Controller;


/**
 * 页面ajax请求服务处理Controller
 * @author Simon
 *
 */
class AjaxServiceController extends Controller {
	
    /**
     * Ajax服务中转类
     */
	public function center($moudle, $util){

        if(in_array($moudle ,array("auth", "city","jobinfo", 'meeting', 'vote'))){
            if(AdminBiz::verifyUser() == false){
                $this->responseFail(5, '非法用户！');
            }
        }

		switch($moudle){
			case 'common':
				$this->commonService($util);
				return;
			case 'auth':
				$this->authService($util);
				return;
            case 'account':
                $this->accountService($util);
                return;
			default:
				$this->responseFail(5, '无法找到服务模块');
		}
		
    }

    /**
     * @param $util
     */
    public function commonService($util)
    {
    	switch ($util) {
    		case 'login' :
    			$lst_en = CommonBiz::getAllProvince();
    			$this->responseSuccess($lst_en);
    			break;
    		case 'register' :
    			$province_id = I('province_id');
    			$lst_en = CommonBiz::getCityListByProvinceId($province_id);
    			$this->responseSuccess($lst_en);
    			break;
            case 'logout':
                $this->responseSuccess('');
                break;
    		default :
    			$this->responseFail(5, '无法找到服务方法');
    			break;
    	}
    }

    /**
     *
     *
     *
     * /**
     * @param $util
     */
    public function accountService($util){
    	switch(strtolower($util)){
    		case 'logout':
                //用户登陆
    			$user_id = I('param.user_id');
    			$error_msg = AdminBiz::logout($user_id);
    			if(empty($error_msg)){
    				$this->responseSuccess();
    			}
    			else{
    				$this->responseFail(5, $error_msg);
    			}
    			return;
    		default:
    			$this->responseFail(5, '无法找到服务方法');
    	}
    }
    
    
    public function authService($util){
    	switch(strtolower($util)){
    		case 'domainquery':
    			//用户登陆
    			$domain_name = I('param.domain_name');
    			$domain_url = I('param.domain_url');
    			$domain_id = I('param.domain_id');
    			 
    			$reultList = AuthBiz::queryDomainByPara($domain_name, $domain_url,$domain_id);
    			$this->responseSuccess($reultList);
    			return;
    			
    		case 'adddomain':

    			$domain_name = I('param.domain_name');
    			$domain_url = I('param.domain_url');
    			$default_url = I('param.default_url');
    			$reultList = AuthBiz::addDomain($domain_name, $domain_url,$default_url);
    			$this->responseSuccess();
    			return;
    		case 'updatedomain':
    			$domain_id = I('param.domain_id');
    			$domain_name = I('param.domain_name');
    			$domain_url = I('param.domain_url');
    			$default_url = I('param.default_url');
    			$reultList = AuthBiz::updateDomain($domain_id,$domain_name, $domain_url,$default_url);
    			$this->responseSuccess();
    				return;
    		case 'resetpassword':
    			$uname = I('param.uname');
    			$originalpass = I('param.originalpass');
    			$newpass = I('param.newpass');
    			$confirmpass = I('param.confirmpass');
    			$reultList = AuthBiz::resetPassword($uname,$originalpass, $newpass,$confirmpass);
    			$this->responseSuccess($reultList);
    				return;
    		case 'menuresourcequery':
    			$menu_id = I('param.menu_id');
    			$menu_name = I('param.menu_name');
    			$menu_domain = I('param.menu_domain');
    			$level = I('param.level');
    			$parent_id = I('param.parent_id');
    			$reultList = AuthBiz::queryMenuResourceByPara($menu_id, $menu_name,$menu_domain,$level,$parent_id);
    			$this->responseSuccess($reultList);
    		    	return;
    	   case 'userquery':
    			$user_name = I('param.user_name');
    		    $user_nick = I('param.user_nick');
    		    $user_mobile = I('param.user_mobile');
    		    $user_id = I('param.user_id');
    		    $reultList = AuthBiz::queryUserByPara($user_name, $user_nick,$user_mobile,$user_id);
    		    $this->responseSuccess($reultList);
    	            return;
    	   case 'groupquery':
    		   	$group_name = I('param.group_name');
    		   	$group_id = I('param.group_id');
    		    $reultList = AuthBiz::queryGroupByPara($group_name, $group_id);
    		    $this->responseSuccess($reultList);
    		        return;
    	   case 'addgroup':
    	    	$group_name = I('param.group_name');
    		    $group_desc = I('param.group_desc');
    		    $menu_relation = I('param.menu_relation');
    		    $user_relation = I('param.user_relation');
    		    $error_msg = AuthBiz::addGroup($group_name, $group_desc,$menu_relation,$user_relation);
    		    if(empty($error_msg)){
    		    $this->responseSuccess();
    		    }else{
    		    	$this->responseFail(5, $error_msg);
    		    }
    		       return;
    	   case 'updatemenuresource':
    		     $menu_id = I('param.menu_id');
    		     $menu_name = I('param.menu_name');
    		     $menu_url = I('param.menu_url');
    		     $menu_display = I('param.menu_display');
    		     $reultList = AuthBiz::updatemenuResourceByMenuId($menu_id, $menu_name,$menu_url,$menu_display);
    		     $this->responseSuccess($reultList);
    		      return;
    	   case 'addmenuresource':
    		     $menu_name = I('param.menu_name');
    		     $menu_domain = I('param.menu_domain');
    		     $menu_url = I('param.menu_url');
    		     $parent_id = I('param.parent_id');
    		     $level = I('param.level');
    		     $menu_display = I('param.menu_display');
    		     $reultList = AuthBiz::addMenuResource( $menu_name,$menu_domain,$menu_url,$parent_id,$level,$menu_display);
    		     $this->responseSuccess($reultList);
    		      return;
    	   case 'removeuserrelation':
    		      $relatn_id = I('param.relatn_id');
    		      AuthBiz::removeUserRelation($relatn_id);
    		      $this->responseSuccess();
    		      return;
    	   case 'removemenurelation':
    		      $relatn_id = I('param.relatn_id');
    	   	      AuthBiz::removeMenuRelation($relatn_id);
    		      $this->responseSuccess();
    		      return;
    		      
    	   case 'addtogrouprelationmenu':
    		      $group_id = I('param.group_id');
    		      $menu_relation = I('param.menu_relation');
    		      AuthBiz::addToGroupRelationMenu($group_id,$menu_relation);
    		      $this->responseSuccess();
    		      return;
    	   case 'addtogrouprelationuser':
    		      $group_id = I('param.group_id');
    		      $user_relation = I('param.user_relation');
    		      AuthBiz::addToGroupRelationUser($group_id,$user_relation);
    		      $this->responseSuccess();
    		      return;
    	   case 'adduser':
    		      $user_name = I('param.user_name');
    		      $user_nick = I('param.user_nick');
    		      $user_mobile = I('param.user_mobile');
    		      $password = I('param.password');
    		      $user_address = I('param.user_address');
    		      $error_msg=AuthBiz::addUser($user_name,$user_nick,$user_mobile,$password,$user_address);
    	          if(empty($error_msg)){
    		       $this->responseSuccess();
    		      }else{
    		    	$this->responseFail(5, $error_msg);
    		      }
    		      return;  
    	   case 'updateuser':
            	  $user_id = I('param.user_id');
    		      $user_name = I('param.user_name');
    		      $user_nick = I('param.user_nick');
    		      $user_mobile = I('param.user_mobile');
    		      $password = I('param.password');
    		      $user_address = I('param.user_address');
    		      AuthBiz::updateUser($user_id,$user_name,$user_nick,$user_mobile,$password,$user_address);
    		      $this->responseSuccess();
    		      	return;
    		case 'userrulequery':
    		      $user_name = I('param.user_name');
    		      $reultList=AuthBiz::userRuleQuery($user_name);
    		      $this->responseSuccess($reultList);
    		      return;
    		case 'deleteuserrule':
    		      $rule_id = I('param.rule_id');
    		      AuthBiz::deleteUserRule($rule_id);
    		      $this->responseSuccess();
    		      return;
    		case 'adduserrulesingle':
    		      $user_name = I('param.user_name');
    		      $lst_menu = I('param.lst_menu');
    		      
    		      $error_msg=AuthBiz::addUserRuleSingle($user_name,$lst_menu);
    		      if(empty($error_msg)){
    		      	$this->responseSuccess();
    		      }else{
    		      	$this->responseFail(5, $error_msg);
    		      }
    		      return;
    		      
     		default:
    			$this->responseFail(5, '无法找到服务方法');
    	}
    }
    

   
    
    public function responseSuccess($result){
    	//0，正确；1，参数错误；2,安全码校验失败;3，无法反序列化；4,服务器错误;5，业务逻辑错误
    	$return_json = array(
    			'IsSuccess' => 1,
    			'ErrorMsg' => '',
    			'ErrorCode' => 0,
    			'Result' => $result,
    	);
    	$this->ajaxReturn($return_json);
    }
    
    public function responseFail($error_code, $error_msg){
    	//0，正确；1，参数错误；2,安全码校验失败;3，无法反序列化；4,服务器错误;5，业务逻辑错误
    	$return_json = array(
    			'IsSuccess' => 0,
    			'ErrorMsg' => $error_msg,
    			'ErrorCode' => $error_code,
    			'Result' => null,
    	);
    	$this->ajaxReturn($return_json);
    }
}